﻿#region Imports

using System;
using Spring.Data.NHibernate.Support;
using Spring.DataBinding;
using Spring.Validation;
using d3PrtDgn.Common.Common;
using d3PrtDgn.Data.IDAO;
using d3PrtDgn.Data.Domain.Entity;

#endregion

namespace d3PrtDgn.Web.User
{
	/// <summary>
	/// User control panel page
	/// </summary>
	public partial class UserCp : Page
	{
		#region Private fields

		private IValidator userValidator;
		private IValidator passwordValidator;
        private Data.Domain.Entity.User user;

		private IUserDao userDao;

		#endregion

		#region Public properties

		/// <summary>
		/// Sets user validator
		/// </summary>
		public IValidator UserValidator
		{
			set { userValidator = value; }
		}

		/// <summary>
		/// Sets password difference validator
		/// </summary>
		public IValidator PasswordValidator
		{
			set { passwordValidator = value; }
		}

		/// <summary>
		/// Gets edited user
		/// </summary>
        public Data.Domain.Entity.User RegisteringUser
		{
			get { return user; }
		}

		/// <summary>
		/// Sets user DAO
		/// </summary>
		public IUserDao UserDao
		{
			set { userDao = value; }
		}

		#endregion

		#region Event handlers

		/// <summary>
		/// Inits page
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			if (SignedInUser.Id == 0)
				SetResult("Login");

			base.OnInit(e);
		}

		/// <summary>
		/// Cancels changes
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Cancel(object sender, EventArgs e)
		{
			SetResult("UserCp");
		}

		/// <summary>
		/// Handles page load event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
		}

		#endregion

		#region Model Management and Data Binding Methods

		protected override void InitializeModel()
		{
			user = SignedInUser;
		}

		protected override void LoadModel(object savedModel)
		{
            user = (Data.Domain.Entity.User)savedModel;
		}

		protected override object SaveModel()
		{
			return user;
		}

		protected override void InitializeDataBindings()
		{
			BindingManager.AddBinding("tbName.Text", "RegisteringUser.Name");
			BindingManager.AddBinding("tbSurname.Text", "RegisteringUser.Surname");
			BindingManager.AddBinding("tbEmail.Text", "RegisteringUser.Email");
			BindingManager.AddBinding("tbUsername.Text", "RegisteringUser.Username", BindingDirection.TargetToSource);
			BindingManager.AddBinding(
				"tbPassword.Text != string.empty ? @(TextUtil).GetMd5Hash(tbPassword.Text) : RegisteringUser.Password",
				"RegisteringUser.Password", BindingDirection.SourceToTarget);

			BindingManager.AddBinding("lblUpdated.Visible", "(Request.Params['updated'] != null)", BindingDirection.TargetToSource);
		}

		#endregion

		#region Private methods

		/// <summary>
		/// Saves user changes
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Save(object sender, EventArgs e)
		{
			if (Validate(this, new IValidator[] {userValidator, passwordValidator}))
			{
				using (new SessionScope())
				{
					userDao.SaveOrUpdate(user);
				}

				SetResult("UserCpUpdated");
			}
		}

		#endregion
	}
}